/**
 * Created by charsen on 16/5/25.
 */
directives.directive('share', ["$rootScope", "tools", "$timeout", "$location", function ($rootScope, tools, $timeout, $location) {
    return {
        restrict: 'AE',
        scope: {},
        replace: true,
        link: function (scope, element, attr) {
            scope.pageName = "";
            scope.accounts = [];
            scope.successfulShared = [];

            var pageId;
            $rootScope.$on('SHARE_EV', function (e, pageId_, pageName) {
                scope.pageName = "";
                scope.accounts = [];
                scope.successfulShared = [];

                tools.get({
                    url: '/Ibm-Client/getAccounts.htm',
                    data: {
                        pageId: pageId_
                    },
                    succ: function (resp) {
                        if (resp.success) {
                            scope.pageName = pageName;
                            scope.accounts = resp.value;
                            scope.accountsCopy = angular.copy(scope.accounts);
                        }
                    }
                });
                $(element).modal('show');
                pageId = pageId_;
            });

            // 点击分享按钮
            scope.share = function () {
                angular.forEach(scope.accounts, function (acc, index) {
                    if (acc.shared) {
                        scope.submit(true, pageId, acc.userId, acc.userName);
                    } else {
                        scope.submit(false, pageId, acc.userId, acc.userName);
                    }
                });
                $timeout(function () {
                    scope.close();
                }, 1800);
            }

            // 提交针对单个用户的分享
            scope.submit = function (flag, pageId, userId, userName, cb) {
                if (flag === true) {
                    tools.get({
                        url: '/Ibm-Client/sharePageForAccount.htm',
                        data: {
                            pageId: pageId,
                            userId: userId
                        },
                        succ: function (resp) {
                            if (resp.success) {
                                scope.successfulShared.push(userName);
                            }
                        }
                    });
                } else {
                    tools.get({
                        url: '/Ibm-Client/cancelSharePage.htm',
                        data: {
                            pageId: pageId,
                            userId: userId
                        },
                        succ: function (resp) {
                            if (resp.success) {

                            }
                        }
                    });
                }
            }

            scope.close = function () {
                $(element).modal('hide');
            }
        },
        templateUrl: "/trend-new-BI/app/components/common/share/shareView.html"
    }
}]);